package gs.android.videopokercollection;

import com.pad.android.xappad.AdController;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private Button newGame, help, resume;
	private SharedPreferences sp;
	private AdController controller;
	
	public void onCreate (Bundle savedInstance){
		super.onCreate(savedInstance);
		setContentView(R.layout.main);
		
		controller = new AdController(this, "185093521");
		controller.loadNotification();
		
		resume = (Button) findViewById(R.id.btnResumeGame);
		resume.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), GameModeActivity.class);
				startActivity(intent);
			}
		});
		
		newGame = (Button) findViewById(R.id.btnNewGame);
		newGame.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int credits = sp.getInt("credits", 0);
				if (credits > 0) {
					AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
			    	builder.setMessage(getResources().getString(R.string.startnewgame))
			    	       .setCancelable(false)
			    	       .setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
			    	           public void onClick(DialogInterface dialog, int id) {
			    	        	   newGame();
			    	           }
			    	       })
			    	       .setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
			    	           public void onClick(DialogInterface dialog, int id) {
			    	        	   dialog.cancel();
			    	           }
			    	       });
			    	AlertDialog alert = builder.create();
			    	alert.show();
				} else {
					newGame();
				}
			}
		});
		
		help = (Button)findViewById(R.id.btnHelp);
		help.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(), HelpActivity.class);
				startActivity(intent);
			}
		});
	}
	
	@Override
	public void onResume() {
		super.onResume();
		sp = getSharedPreferences("poker", MODE_PRIVATE);
		int credits = sp.getInt("credits", 0);
		if (credits > 0)
			resume.setVisibility(View.VISIBLE);
		else
			resume.setVisibility(View.GONE);
	}
	
	private void newGame() {
		Editor editor = sp.edit();
		editor.putInt("credits", 100);
		editor.commit();
		Intent intent = new Intent(this, GameModeActivity.class);
		startActivity(intent);
	}
}
